home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / DUMPCLAS.TAR / util / AttributeInfo.java < prev    next >
Encoding:
Java Source  |  1996-05-22  |  2.9 KB  |  101 lines

  1. /*
  2.  * @(#)AttributeInfo.java    1.4 95/08/16 Chuck McManis
  3.  *
  4.  * Copyright (c) 1996 Chuck McManis, All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies.
  10.  *
  11.  * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
  12.  * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  13.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  14.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS SHALL NOT BE
  15.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  16.  * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  17.  */
  18.  
  19. package util;
  20.  
  21. import java.io.DataInputStream;
  22. import java.io.DataOutputStream;
  23. import java.io.IOException;
  24.  
  25. /**
  26.  * This class defines the generic Attribute type for Java class files.
  27.  * It is a little bit smart in that for some Attributes it can display
  28.  * them intelligently if it also has access to the constant pool of the
  29.  * current class.
  30.  *
  31.  * @version     1.4, 16 Aug 1995
  32.  * @author    Chuck McManis
  33.  * @see        ClassFile
  34.  */
  35. public class AttributeInfo {
  36.     ConstantPoolInfo    name;    // attribute name
  37.     byte        data[]; // attribute's contents
  38.  
  39.     public AttributeInfo(ConstantPoolInfo newName, byte newData[]) {
  40.     name = name;
  41.     data = newData;
  42.     }
  43.  
  44.     public AttributeInfo() {
  45.     }
  46.  
  47.     public boolean read(DataInputStream di, ConstantPoolInfo pool[])
  48.     throws IOException {
  49.     int len;
  50.  
  51.     name = pool[di.readShort()];
  52.     len = di.readInt();
  53.     data = new byte[len];
  54.     len  = di.read(data);
  55.     if (len != data.length)
  56.         return (false);
  57.     return (true);
  58.     }
  59.  
  60.     public void write(DataOutputStream dos, ConstantPoolInfo pool[])
  61.     throws IOException, Exception {
  62.     dos.writeShort(ConstantPoolInfo.indexOf(name, pool));
  63.     dos.writeInt(data.length);
  64.     dos.write(data, 0, data.length);
  65.     }
  66.  
  67.     short indexFromBytes(byte a[]) {
  68.     return (short)(((a[0] << 8) & (0xff << 8)) |
  69.                ((a[1] << 0) & (0xff << 0)));
  70.     }
  71.  
  72.     public String toString(ConstantPoolInfo pool[]) {
  73.     StringBuffer x = new StringBuffer();
  74.     String type = name.toString();
  75.     ConstantPoolInfo item;
  76.  
  77.     if (type.compareTo("ConstantValue") == 0) {
  78.         item = pool[indexFromBytes(data)];
  79.         return (item.toString());
  80.     } else if (type.compareTo("SourceFile") == 0) {
  81.         item = pool[indexFromBytes(data)];
  82.         return (item.toString());
  83.     } else {
  84.         x.append(type+"<"+data.length+" bytes>");
  85.     }
  86.     return (x.toString());
  87.     }
  88.  
  89.     public String toBoolean(ConstantPoolInfo pool[]) {
  90.     ConstantPoolInfo item = pool[indexFromBytes(data)];
  91.  
  92.     if (item.intValue == 0)
  93.         return ("= false");
  94.     return ("= true");
  95.     }
  96.  
  97.     public String toString() {
  98.     return (name.toString()+" <"+data.length+" bytes>");
  99.     }
  100. }
  101.